package com.zhkj.txg.module.mine.ui

import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelFragment
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.adapter.CouponCenterAdapter
import com.zhkj.txg.module.mine.vm.CouponViewModel
import kotlinx.android.synthetic.main.fragment_collection.*

class CouponCenterFragment : ViewModelFragment<CouponViewModel>() {

    companion object {
        val TYPE_ALL = 0
        val TYPE_ALL_AREA = 1
        val TYPE_SHOP = 3
        val TYPE_GOODS = 2
        private val TYPE = "type"
        fun newInstance(type: Int): CouponCenterFragment {
            return CouponCenterFragment().apply {
                arguments = bundleOf(Pair(TYPE, type))
            }
        }
    }

    override fun setViewModel(): CouponViewModel = createViewModel(false)
    override val layoutResId: Int = R.layout.fragment_collection

    private val couponCenterAdapter = CouponCenterAdapter().apply {
        loadMoreModule?.setOnLoadMoreListener {
            viewModel.couponCenterList()
        }
        setOnItemChildClickListener { _, view, position ->
            val coupon = data[position]
            when (view.id) {
                R.id.viewCoupon -> {
                    coupon.shown = coupon.shown.not()
                    notifyItemChanged(position)
                }
                R.id.tvCouponGet -> {
                    viewModel.getCoupon(coupon.id, position)
                }
            }
        }
    }

    override fun initUI() {
        with(rvCollection) {
            (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
            layoutManager = LinearLayoutManager(requireContext())
            adapter = couponCenterAdapter
            setPadding(0, ScreenUtil.dip(context, 20), 0, 0)
            clipToPadding = false
        }
        couponCenterAdapter.setEmptyView(R.layout.layout_empty_data)
        refreshCollection.setOnRefreshListener { viewModel.couponCenterList(true) }
    }

    override fun initData() {
        viewModel.useType = arguments?.getInt(TYPE) ?: 0
        viewModel.couponCenterList()
    }

    override fun initViewModel() {
        // 领取优惠券成功观察者
        viewModel.getCouponLiveData.observe(viewLifecycleOwner, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.let {
                toast(it.msg ?: getString(R.string.coupon_get_success))
                couponCenterAdapter.refreshItem(it.uiPosition)
            }
        })
        // 优惠券列表数据观察者
        viewModel.couponCenterListLiveData.observe(viewLifecycleOwner, Observer { response ->
            when (response.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> response.response?.let {
                    // 判断加载状态
                    if (!it.loadMore) refreshCollection.finishRefresh()
                    else if (it.hasMore) couponCenterAdapter.loadMoreModule?.loadMoreComplete()
                    else couponCenterAdapter.loadMoreModule?.loadMoreEnd(true)
                    // 数据填充
                    if (it.loadMore) couponCenterAdapter.addData(it.data)
                    else couponCenterAdapter.setNewData(it.data)
                }
                HttpResponseStatus.Status.FAILURE -> {
                    response.exception?.printStackTrace()
                    toast(response.statusTip)
                    if (viewModel.page > 1) couponCenterAdapter.loadMoreModule?.loadMoreFail()
                    else refreshCollection.finishRefresh(false)
                }
            }
        })
    }
}